package com.genesis.web.controller.usermember;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.genesis.mall.common.Result;
import com.genesis.mall.dto.SmsCouponHistoryDto;
import com.genesis.mall.entity.*;
import com.genesis.mall.service.*;
import com.genesis.mall.utils.TokenUtil;
import io.jsonwebtoken.Claims;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * 手机端优惠券
 */
@RestController
@RequestMapping("/member/coupon")
public class UmsMemberCouponController {
    @Autowired
    private ISmsCouponService iSmsCouponService;

    @Autowired
    private ISmsCouponHistoryService iSmsCouponHistoryService;

    @Autowired
    private ISmsCouponProductCategoryRelationService iSmsCouponProductCategoryRelationService;

    @Autowired
    private ISmsCouponProductRelationService iSmsCouponProductRelationService;

    @Autowired
    private IPmsProductService iPmsProductService;



    /**
     * 领取指定优惠券
     *
     * @param couponId
     * @return
     */
    @PostMapping("/add/{couponId}")
    public Result add(@PathVariable Long couponId) {

        iSmsCouponService.getById(couponId);
        return Result.success("");
    }

    /**
     * 获取会员优惠券列表
     *
     * @param useStatus
     * @param request
     * @return
     */
    @GetMapping("/list")
    public Result<List<SmsCoupon>> list(@RequestParam int useStatus, HttpServletRequest request) {

        //1.获取token
        String token = request.getHeader("Authorization");
        String token1 = token.substring(6);

        //2.获取
        Claims claims = TokenUtil.getClaims(token1);
        Long umsMemberId = Long.parseLong(claims.get("umsMember").toString());

        LambdaQueryWrapper<SmsCouponHistory> lqw = new LambdaQueryWrapper<>();
        lqw.eq(StringUtils.isNotEmpty(umsMemberId.toString()), SmsCouponHistory::getMemberId, umsMemberId);
        lqw.eq(StringUtils.isNotEmpty("useStatus"), SmsCouponHistory::getUseStatus, useStatus);
        List<SmsCouponHistory> list = iSmsCouponHistoryService.list(lqw);

        ArrayList<SmsCoupon> smsCouponList = new ArrayList<>();
        for (SmsCouponHistory smsCouponHistory : list) {

            LambdaQueryWrapper<SmsCoupon> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(SmsCoupon::getId, smsCouponHistory.getCouponId());
            SmsCoupon smsCoupon = iSmsCouponService.getOne(queryWrapper);
            smsCouponList.add(smsCoupon);

        }
        return Result.success(smsCouponList);

    }

    /**
     * 获取登录会员购物车的相关优惠券
     *
     * @param type
     * @param request
     * @return
     */
    @GetMapping("/list/cart/{type}")
    public Result<List<SmsCouponHistoryDto>> listCart(@PathVariable Integer type, HttpServletRequest request) {


        //1.获取token
        String token = request.getHeader("Authorization");
        String token1 = token.substring(6);

        //2.获取
        Claims claims = TokenUtil.getClaims(token1);
        Long umsMemberId = Long.parseLong(claims.get("umsMember").toString());
        // 优惠券使用、领取历史表
        LambdaQueryWrapper<SmsCouponHistory> lqw = new LambdaQueryWrapper<>();
        lqw.eq(StringUtils.isNotEmpty(umsMemberId.toString()), SmsCouponHistory::getMemberId, umsMemberId);
        List<SmsCouponHistory> list1 = iSmsCouponHistoryService.list(lqw);

        //优惠券表
        //最后封装
        List<SmsCouponHistoryDto> smsCouponHistoryDtoList = new ArrayList<>();


        for (SmsCouponHistory smsCouponHistory : list1) {
            SmsCouponHistoryDto smsCouponHistoryDto = new SmsCouponHistoryDto();

            BeanUtils.copyProperties(smsCouponHistory, smsCouponHistoryDto);  //复制

            LambdaQueryWrapper<SmsCoupon> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(SmsCoupon::getId, smsCouponHistory.getCouponId());
            queryWrapper.eq(SmsCoupon::getType, type);
            //优惠券表
            SmsCoupon smsCoupon = iSmsCouponService.getOne(queryWrapper);
            smsCouponHistoryDto.setSmsCoupon(smsCoupon);

            //优惠券和产品分类关系表
            LambdaQueryWrapper<SmsCouponProductCategoryRelation> lqw2 = new LambdaQueryWrapper<>();
            lqw2.eq(SmsCouponProductCategoryRelation::getCouponId, smsCoupon.getId());
            List<SmsCouponProductCategoryRelation> smsCouponProductCategoryRelationList = iSmsCouponProductCategoryRelationService.list(lqw2);

            smsCouponHistoryDto.setSmsCouponProductCategoryRelationList(smsCouponProductCategoryRelationList);
            // 优惠券和产品的关系表
            LambdaQueryWrapper<SmsCouponProductRelation> lqw3 = new LambdaQueryWrapper<>();
            lqw3.eq(SmsCouponProductRelation::getCouponId, smsCoupon.getId());
            List<SmsCouponProductRelation> smsCouponProductRelationList = iSmsCouponProductRelationService.list(lqw3);

            smsCouponHistoryDto.setProductRelationList(smsCouponProductRelationList);


            smsCouponHistoryDtoList.add(smsCouponHistoryDto);

        }

        return Result.success(smsCouponHistoryDtoList);


    }

    /**
     * 获取当前商品相关优惠券
     *
     * @param productId
     * @param request
     * @return
     */

    @GetMapping("/listByProduct/{productId}")
    public Result<List<PmsProduct>> listByProduct(@PathVariable Long productId, HttpServletRequest request) {


        LambdaQueryWrapper<PmsProduct> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotEmpty(productId.toString()),PmsProduct::getId, productId);

        List<PmsProduct> pmsProductList = iPmsProductService.list(queryWrapper);

        return Result.success(pmsProductList);

    }


    /**
     * 获取会员优惠券历史列表
     * @param useStatus
     * @param request
     * @return
     */
    @GetMapping("/listHistory")
    public  Result<List<SmsCouponHistory>>  listHistory(@RequestParam  int useStatus,HttpServletRequest request){
        //1.获取token
        String authorization = request.getHeader("Authorization");
        String token = authorization.substring(6);
        //2.获取
        Claims claims = TokenUtil.getClaims(token);
        Long umsMemberId = Long.parseLong(claims.get("umsMember").toString());

        //3.优惠价使用.领取历史表
        LambdaQueryWrapper<SmsCouponHistory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotEmpty(umsMemberId.toString()),SmsCouponHistory::getMemberId,umsMemberId);
        queryWrapper.eq(StringUtils.isNotEmpty("useStatus"),SmsCouponHistory::getUseStatus,useStatus);

        List<SmsCouponHistory> smsCouponHistoryList = iSmsCouponHistoryService.list(queryWrapper);

        return Result.success(smsCouponHistoryList);

    }
}
